package code;

import java.util.ArrayList;
import java.util.Stack;

import models.ListNode;
public class PrintListFromTail {
    public ArrayList<Integer> Solution1(ListNode listNode) {
        ArrayList<Integer> re = new ArrayList<>();
        if(listNode == null){
            return re;
        }
        else {
            Stack<Integer> r = new Stack<>();
            while (listNode.next!=null){
                r.push(listNode.val);
                listNode = listNode.next;
            }
            re.add(listNode.val);
            while (r.size()>0){
                re.add(r.pop());
            }
            return re;
        }
    }

    public static void main(String[] argv){
        PrintListFromTail printListFromTail = new PrintListFromTail();
        ListNode a = new ListNode(0);
        ListNode l = a;
        for (int i=0;i<10;i++){
            a.next = new ListNode(i);
            a = a.next;
        }
        ArrayList<Integer> re = printListFromTail.Solution1(l);
        for(Integer val : re){
            System.out.println(val);
        }
    }
}